package com.zizy.healthexpert.Practical;

import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

import com.zizy.healthexpert.Adapter.listAddDiseaseAdapter;
import com.zizy.healthexpert.Config.Config;
import com.zizy.healthexpert.Database.DatabaseUtil;
import com.zizy.healthexpert.Database.MyDataBase_T_FOODS;
import com.zizy.healthexpert.Database.MyDataBase_T_NUTRITION;
import com.zizy.healthexpert.Database.MyDatabase_PersonalInf;
import com.zizy.healthexpert.Public.FoodNutrition;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Objects;

/**
 * Created by Zizy on 2014/5/12.
 */
public   class Practical {

    /*this method if use for calculate food nutrition  （form QR-Code）*/
    public  static String claNutritionFunction(String nutrition){
        //TODO
        return null;
    }

    public static String formatNutrition (int id , int type){
            //TODO

        return null;
    }
    /*getTimeFormat*/
    public static String getTimeFormat(){
        Calendar calendar=Calendar.getInstance();
        int year=calendar.get(Calendar.YEAR);
        int month=calendar.get(Calendar.MONTH)+1;
        int day=calendar.get(Calendar.DATE);

        return String.format("%d/%d/%d",year,month,day);
    }

    /*getDishesTime Chinese Name*/
    public static String getDishesTimeChinese(int time){
        switch (time){
            case 6:case 7:case 8:case 9:case 10:case 5:case 4:case 3:case 2:    case 1:
                return "早餐";
            case 11 :case 12:case 13:case 14:case 15:
                return "中餐";
            case 16 :case 17:case 18:case 19:case 20:  default:
                return "晚餐";
        }
    }
    /*get a list using Strings must separated by comma*/
    public static ArrayList<Integer> getListFromStrings(String str){
        ArrayList<Integer> arrayList=new ArrayList<Integer>();
        String[] strArr=str.split(",");
        for (String s:strArr){
            if (s.equals("") || s==null) continue;
            arrayList.add(Integer.valueOf(s.replace(" ", "")));
        }
        return arrayList;
    }
    /*is food suitable single food*/
    public static int  isFoodSuitable(int singleFoodId,Context context){


        MyDatabase_PersonalInf myDatabase_personalInf=new MyDatabase_PersonalInf(context);
        ArrayList<Object> personalInf=myDatabase_personalInf.SearchDatabase("*",String.format("id=%s",Config.userID));
        /*if not set personal information*/
        if(personalInf.size()==0){
            return Config.NOT_BAD_TO_EAT;
        }
        String strDiseaseIdList=((MyDatabase_PersonalInf.FieldsPersonalInf)personalInf.get(0)).diseaseName;
        String[] arrDiseaseIdList=strDiseaseIdList.split(",");
        for(String str:arrDiseaseIdList){
            if(str.equals("") || str==null) continue;
            int id=Integer.parseInt(str);
            listAddDiseaseAdapter.AddedDisease disease=(listAddDiseaseAdapter.AddedDisease)Config.Disease.list.get(id);
            Integer[] arrForbidFoods = new Integer[0];
            if(disease.forbidToEatId!=null)
                arrForbidFoods=disease.forbidToEatId.toArray(new Integer[disease.forbidToEatId.size()]);
            Integer[] arrSuggestFoods= new Integer[0];
            if(disease.goodToEatId!=null)
                arrSuggestFoods=disease.goodToEatId.toArray(new Integer[disease.goodToEatId.size()]);
            /*if contains forbid food*/
            for (int i=0;i<(arrForbidFoods.length);i++) {  //forbid id id id id id di
                int foodId = singleFoodId;
                if (arrForbidFoods[i].equals(foodId)) {
                    return Config.CAN_NOT_EAT;
                }
            }
            /*if suggest foods contains*/
            for (int i=0;i<(arrSuggestFoods.length);i++) {  //forbid id id id id id di
                int foodId = singleFoodId;
                if (arrSuggestFoods[i].equals(foodId)) {
                    return Config.GOOD_TO_EAT;
                }
            }
        }

        return Config.NOT_BAD_TO_EAT;


    }
    /*is food suitable (QR) */ //TODO
    /*  method: if  food do not contains any forbid food and this food contains suggest foods then called health to eat
    *           if  food contains any forbid foods then called can not to eat
    *           else called not bad to eat
    * */
    public static int isFoodSuitable(ArrayList<Integer> list,Context context){    //list -> an id list .


        MyDatabase_PersonalInf myDatabase_personalInf=new MyDatabase_PersonalInf(context);
        ArrayList<Object> personalInf=myDatabase_personalInf.SearchDatabase("*",String.format("id=%s",Config.userID));
        String strDiseaseIdList=((MyDatabase_PersonalInf.FieldsPersonalInf)personalInf.get(0)).diseaseName;
        String[] arrDiseaseIdList=strDiseaseIdList.split(",");
        for(String str:arrDiseaseIdList){
            if(str.equals("") || str==null) continue;
            int id=Integer.parseInt(str);
            listAddDiseaseAdapter.AddedDisease disease=(listAddDiseaseAdapter.AddedDisease)Config.Disease.list.get(id);
            Integer[] arrForbidFoods = new Integer[0];
            if(disease.forbidToEatId!=null)
                arrForbidFoods=disease.forbidToEatId.toArray(new Integer[disease.forbidToEatId.size()]);
            Integer[] arrSuggestFoods= new Integer[0];
            if(disease.goodToEatId!=null)
                arrSuggestFoods=disease.goodToEatId.toArray(new Integer[disease.goodToEatId.size()]);
            /*if contains forbid food*/
            for (int i=0;i<(arrForbidFoods.length);i++){  //forbid id id id id id di
                for(Iterator iterator=list.iterator();iterator.hasNext();){
                    int foodId=(Integer)iterator.next();
                    if (arrForbidFoods[i].equals(foodId)){
                        return Config.CAN_NOT_EAT;
                    }
                }
            }
            /*if suggest foods contains*/
            for (int i=0;i<(arrSuggestFoods.length);i++){  //forbid id id id id id di
                for(Iterator iterator=list.iterator();iterator.hasNext();){
                    int foodId=(Integer)iterator.next();
                    if (arrSuggestFoods[i].equals(foodId)){
                        return Config.GOOD_TO_EAT;
                    }
                }
            }
        }

        return Config.NOT_BAD_TO_EAT;
    }
    public static double getSuggestNutValue(int id){
        int age=Config.PersonalInf.age;
        int tag=Config.PersonalInf.tag;
        int gender=Config.PersonalInf.gender;
        for (int i=0;i<Config.Dris.list.size();i++) {
            Config.Dris.DrisFields dris = Config.Dris.list.get(i);
            if(dris.ageMax>age && dris.ageMin<age && dris.nutId==id && dris.tags==tag && dris.gender==gender){
                return  Double.parseDouble(String.format("%.2f",dris.nutValue));
            }
        }
        return  -1;
    }
    public static String getNutritionName(int id){
        MyDataBase_T_NUTRITION myDataBase_t_nutrition=new MyDataBase_T_NUTRITION(Config.Application.context);
        ArrayList<MyDataBase_T_NUTRITION.T_NUTRITION> nutList=myDataBase_t_nutrition.SearchT_NUTRITION("*",String.format("_ID=%d",id)) ;
        if(nutList.size()==0)return null;
        return nutList.get(0).NAME;
    }

    public static boolean isPersonalInfExist(Context context){
        MyDatabase_PersonalInf myDatabase_personalInf=new MyDatabase_PersonalInf(context);
        ArrayList<Object> personalInf=myDatabase_personalInf.SearchDatabase("*",String.format("id=%s",Config.userID));
        return personalInf.size()!=0;


    }
    /*is food suitable (dishes) */ //TODO
    /*  method: if  food do not contains any forbid food and this food contains suggest foods then called health to eat
    *           if  food contains any forbid foods then called can not to eat
    *           else called not bad to eat
    *           list: id list (";"split)
    * */
    public static int isDishesSuitable(ArrayList<String> list, final Context context){    //list -> an id list .

        boolean isGood=false;
        MyDatabase_PersonalInf myDatabase_personalInf=new MyDatabase_PersonalInf(context);
        ArrayList<Object> personalInf=myDatabase_personalInf.SearchDatabase("*",String.format("id=%s",Config.userID));
        if ((!Practical.isPersonalInfExist(context))){


            return  Config.NOT_BAD_TO_EAT;
        }
        String strDiseaseIdList=((MyDatabase_PersonalInf.FieldsPersonalInf)personalInf.get(0)).diseaseName;
        String[] arrDiseaseIdList=strDiseaseIdList.split(",");
        for(String str:arrDiseaseIdList){
            if(str.equals("") || str==null) continue;
            int id=Integer.parseInt(str);
            listAddDiseaseAdapter.AddedDisease disease=(listAddDiseaseAdapter.AddedDisease)Config.Disease.list.get(id);
            /*if contains forbid food*/  //1.search the |key word|<-[list] from T_FOODS -> id(maybe not only) 2.get id(List)->call the method [isFoodSuitable]
            for(Iterator iterator=list.iterator();iterator.hasNext();){
                String foodName=(String)iterator.next();
                MyDataBase_T_FOODS myDataBase_t_foods = new MyDataBase_T_FOODS(context);
                ArrayList<MyDataBase_T_FOODS.T_FOODS> listRes = myDataBase_t_foods.SearchT_FOODS("*", String.format("NAME='%s'", foodName));
                ArrayList<Integer> listId = new ArrayList<Integer>();
                for(int i=0;i<listRes.size();i++){
                    MyDataBase_T_FOODS.T_FOODS res=listRes.get(0);
                    if(res==null) break;
                    listId.add(res._ID);
                }
                if(isFoodSuitable(listId,context)==Config.CAN_NOT_EAT) return Config.CAN_NOT_EAT;
                if(isFoodSuitable(listId,context)==Config.GOOD_TO_EAT) isGood=true;
            }
            /*if suggest foods contains*/
            if(isGood)return Config.GOOD_TO_EAT;
            for(Iterator iterator=list.iterator();iterator.hasNext();){
                String foodName=(String)iterator.next();
                MyDataBase_T_FOODS myDataBase_t_foods = new MyDataBase_T_FOODS(context);
                ArrayList<MyDataBase_T_FOODS.T_FOODS> listRes = myDataBase_t_foods.SearchT_FOODS("*", String.format("NAME='%s'", foodName));
                ArrayList<Integer> listId = new ArrayList<Integer>();
                for(int i=0;i<listRes.size();i++){
                    MyDataBase_T_FOODS.T_FOODS res=listRes.get(0);
                    if(res==null) break;
                    listId.add(res._ID);
                }
                if(isFoodSuitable(listId,context)==Config.GOOD_TO_EAT) return Config.GOOD_TO_EAT;
            }
        }

        return Config.NOT_BAD_TO_EAT;
    }
}
